﻿#include "screenscutcheonmanager.h"
#include "screenscutcheonhandler.h"

using namespace EarthView::World::Core;
using namespace EarthView::World::Spatial3D::Controls;



			CScreenScutcheonManager* CScreenScutcheonManager::mpSingleton = NULL;


			CScutcheonMap::CScutcheonMap()
			{

			}

			CScreenScutcheon*& CScutcheonMap::operator[](const EVString &key) 
			{
				return get(key);
			}

			ev_bool CScutcheonMap::push(const EVString &key, CScreenScutcheon *const &val)
			{
				if(mList.find(key) == mList.end())
				{
					mList[key] = (CScreenScutcheon*)val;
					return true;
				}
				else
				{
					return false;
				}
			}

			ev_bool CScutcheonMap::exist(const EVString &key) const
			{
				return mList.find(key) != mList.end();
			}

			CScreenScutcheon*& CScutcheonMap::get(const EVString &key) 
			{
				return mList[key];
			}

			void CScutcheonMap::erase(const EVString &key)
			{
				if(exist(key))
				{
					mList.erase(key);
				}
			}

			ev_size_t CScutcheonMap::size() const
			{
				return mList.size();
			}

			void CScutcheonMap::clear()
			{
				mList.clear();
			}

			ev_bool CScutcheonMap::empty() const
			{
				return mList.empty();
			}

			CScutcheonMap::_Pairib CScutcheonMap::insert(const value_type &val)
			{
				return mList.insert(val);
			}

			CScutcheonMap::iterator CScutcheonMap::find(const EVString &key)
			{
				return mList.find(key);
			}

			CScutcheonMap::const_iterator CScutcheonMap::find(const EVString &key) const
			{
				return mList.find(key);
			}


            CScreenScutcheonManager::CScreenScutcheonManager()
            {
                mpSingleton=this;
                this->mpScutcheon=NULL;
            }


            CScreenScutcheonManager::~CScreenScutcheonManager()
            {
                this->mpSingleton=NULL;
                this->destoryAll();
            }

            bool CScreenScutcheonManager::exist(const EVString& name) 
            {
                if (this->mScutcheonMap.find(name)!=mScutcheonMap.end())
                {
                    return true;
                }
                return false;
            }

            CScreenScutcheon* CScreenScutcheonManager::operator[](const EVString& name) 
            {
                return getScutcheon(name);
            }

            CScreenScutcheon* CScreenScutcheonManager::getScutcheon(const EVString& name) 
            {
                if (this->exist(name))
                {
                    return mScutcheonMap[name];
                }
                return NULL;
            }

            void CScreenScutcheonManager::removeScutcheon(const EVString& name)
            {
                if (this->exist(name))
                {
                    this->mScutcheonMap.erase(name);
                }
            }

            void CScreenScutcheonManager::destoryScutcheon(const EVString& name)
            {
                CScreenScutcheon* pScutcheon=this->getScutcheon(name);
                this->removeScutcheon(name);
                delete pScutcheon;
                pScutcheon=NULL;
            }

            void CScreenScutcheonManager::destoryAll()
            {
                for (CScutcheonMap::iterator itr=mScutcheonMap.begin();itr!=mScutcheonMap.end();itr++)
                {
                    if(itr->second)
                    {
                        delete itr->second;
                        itr->second=NULL;
                    }
                }
                this->mScutcheonMap.clear();
            }

            CScreenScutcheon* CScreenScutcheonManager::createScutcheon(const EVString& name, const EVString& templateName, CGlobeControl* control)
            {
                if (!this->exist(name))
                {
                    this->mpScutcheon=new CScreenScutcheon(name,templateName,control);
                    this->mScutcheonMap.push(name,mpScutcheon);
                    if(!CScreenScutcheonHandler::mIsHandlerAdded)
                    {
                        CScreenScutcheonHandler* handler = CScreenScutcheonHandler::getSingletonPtr();
                        handler->setGlobeControl(control);
                        CScreenScutcheonHandler::mIsHandlerAdded = true;
                    }
                    return mpScutcheon;
                }
                return NULL;
            }

            void CScreenScutcheonManager::removeAll()
            {
                if(this->mScutcheonMap.size()>0)
                {
                    this->mScutcheonMap.erase(mScutcheonMap.begin(),mScutcheonMap.end());
                }
            }

            CScreenScutcheonManager* CScreenScutcheonManager::getSingletonPtr()
            {
                if(!mpSingleton)
                {
                    mpSingleton=EV_NEW CScreenScutcheonManager();
                }
                return mpSingleton;
            }

            CScreenScutcheonManager& CScreenScutcheonManager::getSingleton()
            {
                if(!mpSingleton)
                {
                    mpSingleton=EV_NEW CScreenScutcheonManager();
                }
                return *mpSingleton;
            }

            CScutcheonMap CScreenScutcheonManager::getScutcheonMap() const
            {
                return this->mScutcheonMap;
            }

            void CScreenScutcheonManager::setVisible( const bool& value )
            {
                EV_ASSERT(mScutcheonMap.size()>0);
                for (CScutcheonMap::iterator itr=mScutcheonMap.begin(); itr!=mScutcheonMap.end(); itr++ )
                {
                    if(itr->second)
                    {
                        itr->second->setVisible(value);
                    }
                }
            }
